package com.example.as.Controller;

import com.example.as.Service.RelationService;
import com.example.as.entity.ChatMessage;
import com.example.as.entity.Relation;
import com.example.as.entity.Result;
import com.example.as.entity.User;
import com.example.as.Service.RecordService;
import com.example.as.Service.UserService;
import com.example.as.untils.SocketManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@SuppressWarnings({"all"})
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    RecordService recordService;    // 聊天记录 服务类
    @Autowired
    UserService userService;    // 用户 服务类
    @Autowired
    RelationService relationService;    // 用户关系 服务类

    SocketManager socketManager;    // 信道 管理类

    @GetMapping("/get")
    public Result get() {
        System.out.println("get了一句：泥嚎泥嚎！");
        return Result.success("泥嚎泥嚎！");
    }

    @PostMapping("/post")
    public Result post() {
        System.out.println("post了一句：泥嚎泥嚎！");
        return Result.success("泥嚎泥嚎！");
    }

    @PostMapping("/login_in")
    public Result loginIn(@RequestParam("account") String account,
                          @RequestParam("psw") String psw) {
        List<User> users = userService.loginIn(account, psw);
        if (!users.isEmpty())
            return Result.success(users.get(0).getName());
        return Result.error("登陆时遇到未知错误");
    }

    @PostMapping("/login_up")
    public Result loginUp(@RequestParam("name") String name,
                          @RequestParam("account") String account,
                          @RequestParam("psw") String psw) {
        if (userService.loginUp(name, account, psw) > 0)
            return Result.success();
        return Result.error("注册时遇到未知错误");
    }

    @GetMapping("/userlist")
    public List<User> getUserList(String account) {
        return userService.getUserList(account);
    }

    @PostMapping("/get_history_list")
    public Result getHistoryList(@RequestParam("latestTimeStamp") String latestTimeStamp) {
        List<ChatMessage> historyList;
        if (!latestTimeStamp.equals("")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Timestamp timestamp = null;
            try {
                // 将字符串解析成Date对象
                Date date = sdf.parse(latestTimeStamp);
                // 使用Date对象构造Timestamp
                timestamp = new Timestamp(date.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
                return Result.error("获取历史记录时遇到未知错误");
            }
            historyList = recordService.getHistoryList(timestamp);
        } else {
            historyList = recordService.getAllHistoryList();
        }
        if (!historyList.isEmpty())
            return Result.success(historyList);
        else
            return Result.success("没有最新消息");
    }

    @PostMapping("/get_relation_list")
    public Result getRelationList(@RequestParam("masterAccount") String masterAccount) {
        List<Relation> relationList = relationService.getRelationList(masterAccount);
        if (!relationList.isEmpty())
            return Result.success(relationList);
        else
            return Result.success("没有最新好友关系");
    }

    @PostMapping("/get_user_list")
    public Result getUserFriendList(@RequestParam("masterAccount") String masterAccount) {
        List<User> users = userService.getUserFriendList(masterAccount);
        if (!users.isEmpty())
            return Result.success(users);
        else
            return Result.success("没有最新好友可以更新");
    }

    @PostMapping("/add_relation")
    public Result addRelation(@RequestParam("masterAccount") String masterAccount,
                              @RequestParam("userAccount") String userAccount
    ) {
        if (userService.addFri(masterAccount, userAccount) > 0)
            return Result.success();
        else
            return Result.error("添加失败");
    }

    @PostMapping("/delFri")
    public Result delFri(@RequestParam("account1") String account1,
                         @RequestParam("account2") String account2) {
        if (userService.delFri(account1, account2) > 0)
            return Result.success();
        else
            return Result.error("删除失败");
    }

    @PostMapping("/getUser")
    public Result getUser(@RequestParam("account") String account) {
        List<User> users = userService.getUser(account);
        if (users.size() > 0)
            return Result.success(users);
        else
            return Result.error("查无此人");
    }

    @GetMapping("/addFri")
    public int addFri(String account1, String account2) {
        return userService.addFri(account1, account2);
    }
}
